package com.yuneec.droneservice.parse;

import android.os.Parcel;
import android.os.Parcelable;

/**
 * @author zd.zhang
 * @version 1.0
 */
public class RxBindInfo implements Parcelable {
    public static final int TYPE_NULL = -1;
    public static final int TYPE_SR12S = 0;
    public static final int TYPE_SR12E = 1;
    public static final int TYPE_SR24S = 2;
    public static final int TYPE_RX24 = 3;
    public static final int TYPE_SR19P = 4;

    public int mode;
    public int panId;
    public int nodeId;
    public int aNum;
    public int aBit;
    public int swNum;
    public int swBit;
    public int txAddr; // Add by WHZ, 14/10/26.

    public static final Creator<RxBindInfo> CREATOR = new Creator<RxBindInfo>() {
        @Override
        public RxBindInfo createFromParcel(Parcel in) {
            return new RxBindInfo(in);
        }

        @Override
        public RxBindInfo[] newArray(int size) {
            return new RxBindInfo[size];
        }
    };

    public RxBindInfo() {
    }

    public RxBindInfo(Parcel in) {
        mode = in.readInt();
        panId = in.readInt();
        nodeId = in.readInt();
        aNum = in.readInt();
        aBit = in.readInt();
        swNum = in.readInt();
        swBit = in.readInt();
        txAddr = in.readInt();
    }

    @Override
    public int describeContents() {
        return 0;
    }

    @Override
    public void writeToParcel(Parcel dest, int flags) {
        dest.writeInt(mode);
        dest.writeInt(panId);
        dest.writeInt(nodeId);
        dest.writeInt(aNum);
        dest.writeInt(aBit);
        dest.writeInt(swNum);
        dest.writeInt(swBit);
        dest.writeInt(txAddr);
    }

    public void readFromParcel(Parcel in) {
        mode = in.readInt();
        panId = in.readInt();
        nodeId = in.readInt();
        aNum = in.readInt();
        aBit = in.readInt();
        swNum = in.readInt();
        swBit = in.readInt();
        txAddr = in.readInt();
    }


    public String getName() {
        switch (mode) {
            case TYPE_SR12S:
                return "SR12S_" + String.valueOf(nodeId);
            case TYPE_SR12E:
                return "SR12E_" + String.valueOf(nodeId);
            case TYPE_SR24S:
                return "SR24S_" + String.valueOf(nodeId) + "v1.03";
            case TYPE_RX24:
                return "RX24_" + String.valueOf(nodeId);
            case TYPE_SR19P:
                return "SR19P_" + String.valueOf(nodeId);
            default:
                if (mode >= 105) {
                    return "SR24S_" + String.format("%dv%.2f", nodeId, (float) mode / 100);
                } else {
                    return String.valueOf(nodeId);
                }
        }
    }

    @Override
    public String toString() {
        StringBuilder sb = new StringBuilder("RxBindInfoFeedback");
        sb.append("  mode:").append(mode);
        sb.append("  pan_id:").append(panId);
        sb.append("  node_id:").append(nodeId);
        sb.append("  a_num:").append(aNum);
        sb.append("  a_bit:").append(aBit);
        sb.append("  sw_num:").append(swNum);
        sb.append("  sw_bit:").append(swBit);
        sb.append("  tx_addr:").append(txAddr);
        return sb.toString();
    }

    public byte[] toByte() {
        int len = 44;
        byte data[] = new byte[len];
        data[6] = (byte) (mode & 0xff);
        data[7] = (byte) ((mode & 0xff00) >> 8);
        data[8] = (byte) (panId & 0xff);
        data[9] = (byte) ((panId & 0xff00) >> 8);
        data[10] = (byte) (nodeId & 0xff);
        data[11] = (byte) ((nodeId & 0xff00) >> 8);
        data[20] = (byte) aNum;
        data[21] = (byte) aBit;
        data[24] = (byte) swNum;
        data[25] = (byte) swBit;
        data[len - 2] = (byte) (txAddr & 0xff);
        data[len - 1] = (byte) ((txAddr & 0xff00) >> 8);
        return data;
    }

}
